package com.lagou.edu.controller;

import com.lagou.edu.api.CodeService;
import com.lagou.edu.common.RestResult;
import com.lagou.edu.pojo.Token;
import com.lagou.edu.service.AuthCodeService;
import com.lagou.edu.service.TokenService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.net.http.HttpResponse;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private TokenService tokenService;

    @Reference
    private CodeService codeService;

    @RequestMapping("/register/{email}/{password}/{code}")
    public RestResult register(@PathVariable String email, @PathVariable String password, @PathVariable String code) {
        String s = email + " " + password + " " + code;
        System.out.println(s);

//        String url = "http://lagou-service-code/code/validate/" + email + "/" + code;
////        String url = "http://192.168.0.103:8081/code/validate/" + email + "/" + code;
//
//
//        RestResult validate = restTemplate.getForObject(url, RestResult.class);
        RestResult restResult = new RestResult();

        RestResult validate = codeService.validate(email, code);

        if (tokenService.findTokenByUserEmail(email) != null) { // 已经注册
            restResult.setStatus(2);
            restResult.setMessage("该用户已注册！");
        } else if (validate.getStatus() == 0) {
            if (tokenService.registerUser(email, password)) {
                restResult.setStatus(0);
                restResult.setMessage("注册成功！");
            } else {    // tokenService的注册失败
                restResult.setStatus(1);
                restResult.setMessage("注册失败！");
            }

        } else {   // 验证码原因注册失败
            restResult.setStatus(2);
            restResult.setMessage(validate.getMessage());
        }

        return restResult;
    }

    @RequestMapping("/isRegistered/{email}")
    public boolean isRegistered(@PathVariable String email) {
        Token token = tokenService.findTokenByUserEmail(email);
        return token != null;
    }

    @RequestMapping("/login/{email}/{password}")
    public RestResult login(@PathVariable String email, @PathVariable String password, HttpServletResponse response) {
        RestResult restResult = new RestResult();

        Token token = tokenService.findTokenByUserEmail(email);

        if(token != null && token.getToken() != null && token.getToken().equals(tokenService.generateTokenByEmailPassword(email, password))) {
            restResult.setStatus(0);
            restResult.setMessage("登录成功");
            Cookie cookie = new Cookie("token", token.getToken());
            cookie.setPath("/");
            cookie.setMaxAge(3600);

            response.addCookie(cookie);
        } else {
            restResult.setStatus(1);
            restResult.setMessage("登录失败");
        }

        return restResult;
    }

    @RequestMapping("/info/{token}")
    public RestResult userInfo(@PathVariable String token) {
        RestResult restResult = new RestResult();

        Token tokenObj = tokenService.findToken(token);
        if (tokenObj == null) {
            restResult.setStatus(1);
            restResult.setMessage("未登录");
        } else {
            restResult.setStatus(0);
            restResult.setMessage("");
            restResult.setData(tokenObj.getEmail());
        }


        return restResult;
    }



}
